package com.changgou.user.listener;

import com.alibaba.fastjson.JSON;
import com.changgou.order.pojo.Task;
import com.changgou.user.config.RabbitMQConfig;
import com.changgou.user.service.UserService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class AddPointListener {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = RabbitMQConfig.CG_BUYING_ADDPOINT)
    public void receiveAddPointMessage(String message) {
        System.out.println("用户服务接收到了任务消息");

        //5.接收消息
        Task task = JSON.parseObject(message, Task.class);
        if (task == null || task.getRequestBody() == null) {
            return;
        }

        //6.redis是否存在
        Object value = redisTemplate.boundValueOps(task.getId()).get();
        if (value != null) {
            return;
        }

        //7-11.更新用户积分 放到service里
        int result = userService.updateUserPoint(task);
        if (result == 0) {
            return;
        }

        //12.返回积分更新成功的消息
        rabbitTemplate.convertAndSend(RabbitMQConfig.EX_BUYING_ADDPOINTUSER, RabbitMQConfig.CG_BUYING_FINISHADDPOINT_KEY, JSON.toJSONString(task));
        System.out.println("用户服务向完成添加积分队列发送了一条消息");
    }
}
